# -*- coding:utf-8 -*-
import logging
import os

from redis import StrictRedis

from info import constants
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore


class Config(object):
    """项目配置"""
    DEBUG = True
    JSON_AS_ASCII = False
    SECRET_KEY = os.urandom(24)

    """数据库配置"""
    SQLALCHEMY_DATABASE_URI = "mysql+pymysql://root:zfs123456@127.0.0.1:3306/informations"
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True

    """Redis配置"""
    REDIS_HOST = "127.0.0.1"
    REDIS_PORT = 6379
    REDIS_PASSWORD = ""

    """Session保存配置"""
    SESSION_TYPE = "redis"
    SESSION_USE_SIGNER = True  # 开启session签名
    SESSION_REDIS = StrictRedis(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD)  # 指定session保存的redis
    SESSION_PERMANENT = False  # 设置需要过期
    PERMANENT_SESSION_LIFETIME = constants.SESSION_REDIS_EXPIRES  # 设置过期时间
    SESSION_KEY_PREFIX = ""  # 设置session前缀

    """日志配置"""
    LOG_LEVEL = logging.DEBUG
    LOG_PATH = "logs/info_log.log"
    LOG_FILE_SIZE = 1024 * 1024 * 100
    LOG_FILE_MAX_COUNT = 10
    LOG_ENCODING = "utf-8"

    """定时任务配置"""
    from info.modules.timer.views import timed_write_login_info_count
    from datetime import datetime
    JOBS = [
        {
            "id": "write_login_info_count",
            # "id": str(datetime.now()),
            "func": timed_write_login_info_count,
            "args": None,
            "trigger": {
                "type": "cron",
                "hour": "00",
                "minute": "00",
                "second": "00"
            }
        }
    ]
    # 存储位置
    SCHEDULER_JOBSTORES = {
        "default": SQLAlchemyJobStore(url="mysql+pymysql://root:zfs123456@127.0.0.1:3306/informations")
    }
    # 线程池配置
    SCHEDULER_EXECUTORS = {
        "default": {
            "type": "threadpool",
            "max_workers": 1
        }
    }
    # 配置时区
    SCHEDULER_TIMEZONE = "Asia/Shanghai"
    SCHEDULER_JOB_DEFAULTS = {
        "coalesce": False,
        "max_instances": 3
    }
    # 调度器开关
    SCHEDULER_API_ENABLED = True


class DevelopmentConfig(Config):
    """开发环境配置"""
    DEBUG = True


class ProductionConfig(Config):
    """生产环境配置"""
    DEBUG = False
    LOG_LEVEL = logging.WARNING


class TestingConfig(Config):
    """单元测试环境配置"""
    DEBUG = True
    TESTING = True


config = {
    "development": DevelopmentConfig,
    "production": ProductionConfig,
    "testing": TestingConfig
}
